/*
    curlplusplus - a c++ wrapper around libcurl
    Copyright (C) 2012  Francesco "Franc[e]sco" Noferi (francesco1149@gmail.com)

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public
    License along with this library; if not, write to the
    Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
    Boston, MA  02110-1301, USA.
*/

#include "../include/curlplusplus/curlpp.hpp"
#include <iostream>
#include <stdlib.h>

namespace curlpp
{
    // functions
    void free(char *ptr)
    {
        curl_free(ptr);
    }

    time_t getdate(char *datestring)
    {
        return curl_getdate(datestring, NULL);
    }

    char *version()
    {
        return curl_version();
    }

    curl_version_info_data *version_info(CURLversion type)
    {
        return curl_version_info(type);
    }

    // exception methods
    exception::exception(std::string info) throw()
    {
        this->info = info;
    }

    exception::~exception() throw()
    {
        // Lonely and empty destructor ;-;
    }

    const char *exception::what() const throw()
    {
        return info.c_str();
    }

    /**
     * NOTE: This is an internal, do not touch if you don't know what you're doing.<br />
     * This class ensures that the curl environment is initialized as soon
     * as an instance of the class is created and cleaned up as soon as the
     * instance is destroyed.
     * Do not instantiate this more than once and make sure you instantiate it
     * before using anything curl-related.<br />
     * This is internally instantiated in curlpp::_env.
     * @see curlpp::_env
     */
    class _curl_env
    {
    public:
        /**
         * Default constructor. Initializes the cURL environment.
         */
        _curl_env() throw()
        {
            if (curl_global_init(CURL_GLOBAL_ALL) != 0)
            {
                std::cerr << "curlpp: failed to set up environment" << std::endl;
                exit(1);
            }
        }

        /**
         * Default destructor. Cleans up the cURL environment.
         */
        virtual ~_curl_env() throw()
        {
            curl_global_cleanup();
        }
    };

    static _curl_env _env; /**< A static instance of the _curl_env class, which ensures
                                that the constructor is called as soon as the program runs
                                and the destructor is called when the program is closing. */
}
